package com.sinping.qh.aspect;

import com.sinping.qh.api.support.Const;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 对http请求进行统一验证
 * @author zhangjin
 * @create 2018/6/9
 */
@Aspect
@Component
public class HttpAspect {


    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @Autowired
    HttpServletRequest httpServletRequest;

    /**
     * 切入点
     */
    @Pointcut("(execution(public * com.sinping.qh.api.app.version.v1.*.*.*(..))) " +
            "||(execution(public * com.sinping.qh.api.admin.*.*.*(..)))" +
            "||(execution(public * com.sinping.qh.api.auth.*.*(..)))")
    public void valid()
    {

    }

    @Before("valid()")
    public void doBefore(JoinPoint joinPoint)
    {
        String token = httpServletRequest.getHeader(Const.TOKEN_NAME);
        String method = joinPoint.getStaticPart().toShortString();
        logger.info("==========================开始 调用方法：{} ,token {}", method, token);
        for (int i = 0; i < joinPoint.getArgs().length; i++) {
            logger.info("======================入参:{} ",joinPoint.getArgs()[i].toString());
        }
    }

}
